/**
 * 公共方法 author collect 
 */

//  yy-mm-dd hh:mm:ss
function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// yy-mm-dd
function formatTimeType2(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}


function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

//数组去重
function uniq(array) {
    let a = [];
    for (var i in array) {
        if (a.indexOf(array[i]) == -1) {
            a.push(array[i])
        }
    }
    return a;
}

//验证手机
function isMobile(phoneNum) {
    var exp = /^1[0-9]{10}$/;
    return exp.test(phoneNum)
}



//这是什么
function time2date(micro_second) {
    var time = {};
    // 总秒数
    var second = Math.floor(micro_second);
    // 天数
    time.day = PrefixInteger(Math.floor(second / 3600 / 24), 2);
    // 小时
    time.hour = PrefixInteger(Math.floor(second / 3600 % 24), 2);
    // 分钟
    time.minute = PrefixInteger(Math.floor(second / 60 % 60), 2);
    // 秒
    time.second = PrefixInteger(Math.floor(second % 60), 2);

    var newtime = '';
    if (time.day > 0) {
        newtime = time.day + '天' + time.hour + '小时' + time.minute + '分' + time.second + '秒';
    } else {
        if (time.hour != 0) {
            newtime = time.hour + '小时' + time.minute + '分' + time.second + '秒';
        } else {
            newtime = time.minute + '分' + time.second + '秒';
        }
    }
    return newtime;
}

//不足位数前面补0
function PrefixInteger(num, length) {
    return (Array(length).join('0') + num).slice(-length); 
}


//秒杀列表倒计时
function seckillCountDown(that) {
    var seckill = that.data.seckill;
    var nowTime = new Date().getTime();
    for (var i = 0; i < seckill.length; i++) {
        var endTime = seckill[i].etime * 1000 || [];
        var total_micro_second = endTime - nowTime || [];
        if (total_micro_second <= 0) {
            seckill[i].lasttime = '已经结束';
        } else {
            seckill[i].lasttime = dateformat(total_micro_second)
        }
    }
    // 渲染倒计时时钟
    that.setData({
        seckill: seckill
    });
    setTimeout(function () {
        total_micro_second -= 1000;
        seckillCountDown(that);
    }, 1000)
}



//团购/秒杀列表倒计时
function groupCountDown(that) {
    var group = that.data.group;
    var nowTime = new Date().getTime();
    for (var i = 0; i < group.length; i++) {
        var startTime = group[i].stime * 1000 || [];
        if (startTime - nowTime > 0) {
            group[i].lasttime = '即将开始';
        } else {
            var endTime = group[i].etime * 1000 || [];
            var total_micro_second = endTime - nowTime || [];
            if (total_micro_second <= 0) {
                group[i].lasttime = '已经结束';
            } else {
                group[i].lasttime = dateformat(total_micro_second)
            }
        }
    }

    // 渲染倒计时时钟
    that.setData({
        group: group
    });
    setTimeout(function () {
        total_micro_second -= 1000;
        groupCountDown(that);
    }, 1000)
}



//团购/秒杀详情倒计时
function groupDetailCountDown(that) {
    var goodsInfo = that.data.goodsInfo;
    var nowTime = new Date().getTime();
    var endTime = goodsInfo.etime * 1000 || [];
    var total_micro_second = endTime - nowTime || [];
    if (total_micro_second <= 0) {
        goodsInfo.lasttime = '已经结束';
    } else {
        goodsInfo.lasttime = dateformat(total_micro_second)
    }

    // 渲染倒计时时钟
    that.setData({
        goodsInfo: goodsInfo
    });
    setTimeout(function () {
        total_micro_second -= 1000;
        groupDetailCountDown(that);
    }, 1000)
}


// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
    var time = {};
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    time.day = PrefixInteger(Math.floor(second / 3600 / 24), 2);
    // 小时
    time.hour = PrefixInteger(Math.floor(second / 3600 % 24), 2);
    // 分钟
    time.minute = PrefixInteger(Math.floor(second / 60 % 60), 2);
    // 秒
    time.second = PrefixInteger(Math.floor(second % 60), 2);
    return time;
}

//不足位数前面补0
function PrefixInteger(num, length) {
    return (Array(length).join('0') + num).slice(-length);
}






// 导出
module.exports = {
    formatTime:formatTime,
    formatTimeType2:formatTimeType2,
    uniq:uniq,
    isMobile:isMobile,
    time2date:time2date,
    seckillCountDown:seckillCountDown,
    groupCountDown:groupCountDown,
    groupDetailCountDown:groupDetailCountDown
}



